<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/2/17
 * Time: 16:51
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class ApprovalProcessModel
{
    const TABLE = 'approval_process';

    /**
     * @param array $data
     * @return array
     */
    public function info(array $data){
        $result = DB::table(self::TABLE)
            ->where(function($query)use($data){
                if (!empty($data['order'])){
                    $query->where(self::TABLE.'.order',$data['order']);
                }
                if (!empty($data['id'])){
                    $query->where(self::TABLE.'.id',$data['id']);
                }
                if (!empty($data['approvalId'])){
                    $query->where(self::TABLE.'.approvalId',$data['approvalId']);
                }
                if (isset($data['status']) &&$data['status'] >= 0){
                    $query->where(self::TABLE.'.status',$data['status']);
                }
                if (!empty($data['adminId'])){
                    $query->where(self::TABLE.'.adminId',$data['adminId']);
                }
                if (!empty($data['roleId'])){
                    $query->where(self::TABLE.'.roleId',$data['roleId']);
                }
            })
            ->first();
        return empty($result)?[]:get_object_vars($result);
    }

    /**
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function update(array $where,array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }

    /**
     * @param array $data
     * @return array
     */
    public function getProcess(array $data)
    {
        $result = DB::table(self::TABLE)
            ->leftJoin('admin as a','a.id','=',self::TABLE.'.adminId')
            ->where(function($query)use($data){
                if (!empty($data['approvalId'])){
                    $query->where(self::TABLE.'.approvalId',$data['approvalId']);
                }
//                $query->where(self::TABLE.'.status','!=',0);
            })
            ->select(self::TABLE.'.*','a.name')
            ->orderBy('process')
            ->get()->toArray();
        return $result;
    }

    /**
     * @param array $processData
     * @return mixed
     */
    public function insert(array $processData)
    {
        return DB::table(self::TABLE)->insert($processData);
    }

    public function lists(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('role as r','r.id','=',self::TABLE.'.roleId')
            ->where(function($query)use($data){
                if (!empty($data['approvalId'])){
                    $query->where('approvalId',$data['approvalId']);
                }
            })
            ->select(self::TABLE.'.*','r.name as roleName')
            ->get()->toArray();
    }
}